package com.pjb.service.impl;

import com.pjb.service.IBlogService;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: zhaoyan5
 */
public class BlogService implements IBlogService {
    /**
     * Elasticsearch高级客户端
     */
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 创建索引
     *
     * @param indexName 索引名称
     */
    @Override
    public boolean createIndex(String indexName) {
        boolean result = false;

        //参数验证
        if (indexName == null || indexName.length() <= 0) {
            return false;
        }

        try {
            //1.创建索引的请求
            CreateIndexRequest request = new CreateIndexRequest(indexName);
            //构建索引结构
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                builder.startObject("properties");
                {
                    //创建博客ID文档字段
                    builder.startObject("blogId");
                    {
                        builder.field("type", "long");
                    }
                    builder.endObject();

                    //创建博客名称文档字段
                    builder.startObject("blogName");
                    {
                        builder.field("type", "text");
                    }
                    builder.endObject();

                    //创建博客地址文档字段
                    builder.startObject("blogUrl");
                    {
                        builder.field("type", "keyword");
                    }
                    builder.endObject();

                    //创建博客积分字段
                    builder.startObject("blogPoints");
                    {
                        builder.field("type", "double");
                    }
                    builder.endObject();

                    //创建创建时间字段
                    builder.startObject("createDate");
                    {
                        builder.field("type", "date");
                    }
                    builder.endObject();

                    //创建博客描述字段
                    builder.startObject("blogDescribe");
                    {
                        builder.field("type", "text")
                                //插入时分词
                                .field("analyzer", "ik_smart")
                                //搜索时分词
                                .field("search_analyzer", "ik_max_word");
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
            request.mapping(builder);

            //2客户端执行请求，请求后获得响应
            CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);

            result = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }

}
